package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyMPSDao;

public class CompanyMPSDaoImpl extends HibernateDaoSupport implements
		CompanyMPSDao {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Map<String, Object>> getMPS(final String menuName,final String comId,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		Object mps=this.getHibernateTemplate().execute(new HibernateCallback() {

			@SuppressWarnings("deprecation")
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select sum(mps.com_total_profit) as profit,sum(mps.total_num) as totalNum,mps.menu_name as menuName,mps.s_date as sDate  "
						+ " from jm_menu_profit_summary as mps where mps.com_id='"+comId+"'";
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and mps.s_date>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and mps.s_date<='"+e_date+"'";
				}
				if(menuName!=null&&!menuName.equals("")){
					sql+=" and mps.menu_name like '%"+menuName+"%'";
				}
				sql+=" group by mps.menu_id order by mps.s_date desc";
				Query query=session.createSQLQuery(sql).addScalar("profit", Hibernate.DOUBLE).addScalar("totalNum",Hibernate.INTEGER).addScalar("menuName",Hibernate.STRING).addScalar("sDate",Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
				return query.list();
			}
		});
		return (List<Map<String, Object>>) mps;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Map<String, Object>> getComMPS(final int initPage, final int pageRow,
			final String menuName,final String comId,final String s_date,final String e_date,final String order,
			final String sort) {
		// TODO Auto-generated method stub
		Object mps=this.getHibernateTemplate().execute(new HibernateCallback() {

			@SuppressWarnings("deprecation")
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select sum(mps.com_total_profit) as profit,sum(mps.total_num) as totalNum,mps.menu_name as menuName,mps.s_date as sDate  "
						+ " from jm_menu_profit_summary as mps where mps.com_id='"+comId+"'";
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and mps.s_date>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and mps.s_date<='"+e_date+"'";
				}if(menuName!=null&&!menuName.equals("")){
					sql+=" and mps.menu_name like '%"+menuName+"%'";
				}
				sql+=" group by mps.menu_id,mps.s_date ";
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					sql+=" order by "+order+" "+sort;
				}else{
					sql+=" order by mps.s_date desc";
				}
				Query query=session.createSQLQuery(sql).addScalar("profit", Hibernate.DOUBLE).addScalar("totalNum",Hibernate.INTEGER).addScalar("menuName",Hibernate.STRING).addScalar("sDate",Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Map<String, Object>>) mps;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int mpsCount(final String menuName,final String comId,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select sum(mps.com_total_profit) as profit,sum(mps.total_num) as totalNum,mps.menu_name as menuName,mps.s_date as sDate  "
						+ " from jm_menu_profit_summary as mps where mps.com_id='"+comId+"'";
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and mps.s_date>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and mps.s_date<='"+e_date+"'";
				}
				if(menuName!=null&&!menuName.equals("")){
					sql+=" and mps.menu_name like '%"+menuName+"%'";
				}
				Query query=session.createSQLQuery(sql);
				return query.list().size();
			}
		});
		return count;
	}

}
